package com.putong35.common.customviews.iconcontent;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.putong35.common.R;
import com.putong35.common.utils.DimensionUtil;


/**
 *
 * 基于RichTextBar， 并将Extra部分用来展示text，tail部分展示arrow
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: long long ago
 * @UpdateDate: 2016-06-28
 * @UpdateDate: 2017-05-06
 * @Version: 1.1.0
 */
public class RichExtraTextBar extends RichTextBar {

    public RichExtraTextBar(Context context) {
        this(context, null);
        setupViews(null);
    }


    public RichExtraTextBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupViews(attrs);
    }


    private void setupViews(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RichExtraTextBar);

            // extra text
            String extraText = a.getString(R.styleable.RichExtraTextBar_customview_extraText);
            int extraTextColor = a.getColor(R.styleable.RichExtraTextBar_customview_extraTextColor, INVALIDE_VALUE);
            float extraTextSize = a.getDimension(R.styleable.RichExtraTextBar_customview_extraTextSize, INVALIDE_VALUE);
            float extraTxtPaddingRight = a.getDimension(R.styleable.RichExtraTextBar_customview_extraTextPaddingRight, INVALIDE_VALUE);
            float extraTxtPaddingLeft = a.getDimension(R.styleable.RichExtraTextBar_customview_extraTextPaddingLeft, INVALIDE_VALUE);
            int gravity = a.getInt(R.styleable.RichExtraTextBar_customview_extraTextGravity, DEF_EXTRA_GRAVITY);
            setExtraText(extraText, extraTextColor, extraTextSize, extraTxtPaddingLeft, extraTxtPaddingRight, gravity);

            // set arrow view
            Drawable arrowDraw = a.getDrawable(R.styleable.RichExtraTextBar_customview_arrow);
            if (arrowDraw != null) {
                ImageView ivArrow = new ImageView(getContext());
                ivArrow.setImageDrawable(arrowDraw);
                ivArrow.setPadding(0, 0, 0, 0);
                int arrowWidth = (int) a.getDimension(R.styleable.RichExtraTextBar_customview_arrowWidth, DimensionUtil.dip2px(getContext(), 20));
                int arrowHeight = (int) a.getDimension(R.styleable.RichExtraTextBar_customview_arrowWidth, DimensionUtil.dip2px(getContext(), 30));
                LinearLayout.LayoutParams arrowParam = new LinearLayout.LayoutParams(arrowWidth, arrowHeight);
                getTailLayout().addView(ivArrow, arrowParam);
            }
            a.recycle();
        }

    }


    // region set extra text api
    private void setExtraText(CharSequence extraText, int extraTxtColorValue, float extraTxtSize, float extraTextPaddingLeft, float extraTextPaddingRight, int layoutGravity) {
        TextView extraTextView = getExtraTextView();
        if (extraTextView == null) {
            getExtraLayout().removeAllViews();
            extraTextView = new TextView(getContext());
            extraTextView.setGravity(Gravity.CENTER);
            LayoutParams txtParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            txtParams.gravity = Gravity.CENTER_VERTICAL;
            extraTextView.setSingleLine();
            addExtraView(extraTextView);
        }
        extraTextView.setText(extraText);
        setExtraTextViewGravity(Gravity.CENTER_VERTICAL | layoutGravity);

        if (extraTxtColorValue != INVALIDE_VALUE) {
            extraTextView.setTextColor(extraTxtColorValue);
        }
        if (extraTxtSize != INVALIDE_VALUE) {
            extraTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, extraTxtSize);
        }
        int paddingLeft = 0, paddingTop = 0, paddingRight = 0, paddingBottom = 0;
        paddingTop = extraTextView.getPaddingTop();
        paddingBottom = extraTextView.getPaddingBottom();
        if (extraTextPaddingLeft != INVALIDE_VALUE) {
            paddingLeft = (int)extraTextPaddingLeft;
        } else {
            paddingLeft = extraTextView.getPaddingLeft();
        }
        if (extraTextPaddingRight != INVALIDE_VALUE) {
            paddingRight = (int)extraTextPaddingRight;
        } else {
            paddingRight = extraTextView.getPaddingRight();
        }

        extraTextView.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
        extraTextView.setVisibility(VISIBLE);
    }


    public void setExtraText(CharSequence chars) {
        setExtraText(chars, INVALIDE_VALUE, INVALIDE_VALUE, INVALIDE_VALUE,INVALIDE_VALUE, getExtraTextGravity());
    }


    public void setCustomview_extraText(CharSequence chars) {
        setExtraText(chars);
    }

    public void setExtraText(CharSequence chars, int gravity) {
        setExtraText(chars, INVALIDE_VALUE, INVALIDE_VALUE, INVALIDE_VALUE,INVALIDE_VALUE, gravity);
    }

    public void setExtraTextColor(int color) {
        TextView tv = getExtraTextView();
        if (tv != null) {
            tv.setTextColor(color);
        }
    }


    public TextView getExtraTextView() {
        TextView tvExtraView = null;
        if (getExtraLayout().getChildCount() > 0 && getExtraLayout().getChildAt(0) instanceof TextView) {
            tvExtraView = (TextView) getExtraLayout().getChildAt(0);
        }
        return tvExtraView;
    }


    public int getExtraTextGravity() {
        TextView subTv = getExtraTextView();
        if (subTv == null) {
            return DEF_EXTRA_GRAVITY;
        } else {
            return ((LayoutParams) subTv.getLayoutParams()).gravity;
        }
    }


    public void setExtraTextViewGravity(int gravity) {
        if (getExtraTextGravity() != gravity) {
            getExtraLayout().setGravity(gravity);
        }
    }

    // endregion

}
